﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace WebUI.Pages
{
    public partial class editTeam : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Get credentials
            UserController usrController = new UserController();
            int secUsuario = usrController.GetUserID(User.Identity.Name);
            int secTeam = usrController.GetUserTeamID(secUsuario);

            if (secTeam != 9)
            {
                Response.Redirect("../Pages/403.aspx");
            }             
            
            if (!this.IsPostBack)
            {
                TeamController tmController = new TeamController();

                int idTime = Convert.ToInt32(Request.QueryString["id"]);
                string Time = tmController.GetTeamName(idTime);
                int idGestor = tmController.GetTeamGestor(idTime);

                ddlGestor.DataSource = usrController.GetTeamUsers(9);
                ddlGestor.DataValueField = "idUsuario";
                ddlGestor.DataTextField = "Nome";
                ddlGestor.DataBind();
                ddlGestor.SelectedValue = idGestor.ToString();

                lblID.Text = idTime.ToString();
                txtNome.Text = Time;
            }
        }

        protected void cmdSalvar_Click(object sender, EventArgs e)
        {
            TeamController tmController = new TeamController();
            UserController usrController = new UserController();

            int idTime = Convert.ToInt32(Request.QueryString["id"]);
            string Time = txtNome.Text;
            int idGestor = Convert.ToInt32(ddlGestor.SelectedValue);

            tmController.UpdateTeam(idTime, Time, idGestor);
            Response.Redirect("../Pages/mgrTeams.aspx");
        }
    }
}